package com.dragon.mmochat.model.battle.record;

import java.util.ArrayList;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.message.Mmochat_3_12S_Record;
import com.dragon.mmochat.message.Mmochat_3_13S_Record;
import com.dragon.mmochat.message.Mmochat_3_1S_Record;
import com.dragon.mmochat.message.Mmochat_3_3S;
import com.dragon.mmochat.message.Mmochat_3_4S;
import com.dragon.mmochat.message.Mmochat_3_5S;
import com.dragon.mmochat.model.battle.MmochatBattle;
import com.dragon.mmochat.model.battle.MmochatRoundFrame;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatBattleService;
import com.dragon.mmochat.service.MmochatMainService;
import com.dragon.mmochat.util.MmochatUtil;

//战斗录像播放器
public class MmochatBattleRecordPlayer {
	private int roleId;// 观看录像的用户ID
	private int battleRecordId; // 指向战斗录像对像引用
	private long playStartTick = System.currentTimeMillis();// 开始播放时间点
	private int curTurn = 0;// 当前观看到第几回合

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public int getBattleRecordId() {
		return battleRecordId;
	}

	public void setBattleRecordId(int battleRecordId) {
		this.battleRecordId = battleRecordId;
	}

	public long getPlayStartTick() {
		return playStartTick;
	}

	public void setPlayStartTick(long playStartTick) {
		this.playStartTick = playStartTick;
	}

	public int getCurTurn() {
		return curTurn;
	}

	public void setCurTurn(int curTurn) {
		this.curTurn = curTurn;
	}

	// 下发初始化战斗场景
	public void start() {
		MmochatPlayer me = MmochatMainService.getRoleFromBuffer(roleId);
		if (me != null) {
			MmochatBattleRecord battle = MmochatBattleService.battleRecords
					.get(battleRecordId);
			if (battle != null) {
				Mmochat_3_1S_Record pack = new Mmochat_3_1S_Record();
				pack.setBattle(battle);
				pack.setMapId(me.getMapId());
				pack.setLine((byte) me.getLine());
				pack.setMyPos((byte) 0);
				pack.setAudience(true);
				MmochatUtil.sendCommonPack(me, pack);
			}
		}
	}

	// 返回是否战斗结束
	public boolean play() {
		MmochatPlayer me = MmochatMainService.getRoleFromBuffer(roleId);
		if (me != null) {
			MmochatBattleRecord battle = MmochatBattleService.battleRecords
					.get(battleRecordId);
			if (battle != null) {
				long curTimeCost = System.currentTimeMillis() - playStartTick;
				long recordTime = battle.getStartTick() + curTimeCost;
				// 战斗聊天
				for (MmochatBattleChatRecord chat : battle.getChats()) {
					if (chat.getStartTick() <= recordTime
							&& recordTime - chat.getStartTick() < 2000) {
						Mmochat_3_5S pack = new Mmochat_3_5S();
						pack.setBattleId(battleRecordId);
						pack.setChat(chat.getChat());
						MmochatUtil.sendCommonPack(me, pack);
					}
				}

				// 闹钟消失记录
				for (MmochatBattleActionClockRecord clock : battle.getClocks()) {
					if (clock.getTick() <= recordTime
							&& recordTime - clock.getTick() < 2000) {
						Mmochat_3_4S pack = new Mmochat_3_4S();
						pack.setPos(clock.getPos());
						MmochatUtil.sendCommonPack(me, pack);
					}
				}

				// 战斗每回合数据
				for (MmochatBattleRoundRecord round : battle.getRounds()) {
					if (round.getStartTick() <= recordTime
							&& curTurn + 1 == round.getTurn()) {
						curTurn++;

						Mmochat_3_12S_Record pack1 = new Mmochat_3_12S_Record();
						pack1.setBattle(round);
						pack1.setPetId(null);
						pack1.setMe(me);
						MmochatUtil.sendCommonPack(me, pack1);

						// 分批下发操作序列
						List<MmochatRoundFrame> frames = new ArrayList<MmochatRoundFrame>();
						int count = 0;
						int totalAttackNum = round.getFrames().size();
						for (MmochatRoundFrame frame : round.getFrames()) {
							frames.add(frame);
							count++;
							if (count >= 5) {
								Mmochat_3_13S_Record pack2 = new Mmochat_3_13S_Record();
								pack2.setBattle(round);
								pack2.setPetId(null);
								pack2.setTotalAttackNum(totalAttackNum);
								pack2.setFrames(frames);
								pack2.setMe(me);
								MmochatUtil.sendCommonPack(me, pack2);
								// 重置
								frames.clear();
								count = 0;
							}
						}
						if (frames.size() > 0) {
							Mmochat_3_13S_Record pack2 = new Mmochat_3_13S_Record();
							pack2.setBattle(round);
							pack2.setPetId(null);
							pack2.setTotalAttackNum(totalAttackNum);
							pack2.setFrames(frames);
							pack2.setMe(me);
							MmochatUtil.sendCommonPack(me, pack2);
						}
						if (round.isOver()) {
							return true;
						}
						break;
					}
				}
			}
		}
		return false;
	}

	// 强制退出观看
	public void quit() {
		MmochatPlayer me = MmochatMainService.getRoleFromBuffer(roleId);
		if (me != null) {
			MmochatBattle endBattle = new MmochatBattle();
			endBattle.setBattleId(battleRecordId);
			endBattle.setTurn(curTurn + 1);
			endBattle.setOver();
			Mmochat_3_3S pack = new Mmochat_3_3S();
			pack.setBattle(endBattle);
			MmochatUtil.sendCommonPack(me, pack);
		}
	}
}
